<script setup>
import { storeToRefs } from 'pinia';
const dayjs = useDayjs()

const urlHome = useUrl();

const props = defineProps({
	doc: String,
	mpp_doc: Number,
	nars: Array,
	sum_close: Number,
	usls: Array,
	sum_usl: Number,
	dn: String,
	dk: String,
	opl: Array,
	lgot: String,
	year: Number,
	month: Number,
	podrSelect: Number
})

const pend = ref('')
// pend.value = false;

const nars_numbers = ref([]);

const nars_lab_ok = ref([])
const usl_numbers = ref([]);


const nars_usl_ok = ref([])
const comment = ref(false)
comment.value = false

const lab_nars_tech = ref([])
const usl_nars_tech = ref([])

const lab_finish_all = ref([])
const usl_finish_all = ref([])

const stu_lab_tech = ref(0)
const stu_usl_tech = ref(0)

const stu_lab_finish = ref(0)
const stu_usl_finish = ref(0)

const accept_btn = ref(false)

const mpp = useCookie('mpp');

const reportStStore = useStReport();
const { countLoadingDesc, addUslSum, addLabSum } = reportStStore;


async function set_report_by_doc() {
	let formData = new FormData();
	const nars_lab_ok_json = JSON.stringify(nars_lab_ok.value)
	const nars_usl_ok_json = JSON.stringify(nars_usl_ok.value)
	const nars_numbers_json = JSON.stringify(nars_numbers.value)
	const nars_usl_numbers_json = JSON.stringify(usl_numbers.value)
	formData.append("mpp", props.mpp_doc)
	formData.append("nars_lab_all", nars_numbers_json)
	formData.append("nars_lab_ok", nars_lab_ok_json)
	formData.append("usl_numbers_all", nars_usl_numbers_json)
	formData.append("nars_usl_ok", nars_usl_ok_json)
	formData.append("dn", props.dn)
	formData.append("dk", props.dk)
	formData.append("opl", props.opl)
	formData.append("year", props.year)
	formData.append("month", props.month)



	const { data: usl_new } = await useFetch(urlHome.value + 'naryad/zav_report_finish/', {
		onRequest({ request, options }) {
			options.headers = options.headers || {},
				options.method = 'POST',
				options.body = formData;
			// pend.value = true;
		},
		onResponse({ request, response, options }) {

			// console.log("zav_report_finish_response: ", response._data)

			accept_btn.value = false
			get_tech_report_6();
			get_report_2();
			return response._data
		}
	})
	// pend.value = false;
}


async function get_tech_report_6() {
	let formData = new FormData();
	formData.append("mpp", props.mpp_doc)
	formData.append("dn", props.dn)
	formData.append("dk", props.dk)
	formData.append("opl", props.opl)
	formData.append("year", props.year)
	formData.append("month", props.month)
	formData.append("lgot", props.lgot)

	const { data: nar_tech } = await useFetch(urlHome.value + 'naryad/get_stat_for_zav2/' + props.mpp_doc + '/6/' + props.podrSelect + '/' + mpp.value + '/', {
		onRequest({ request, options }) {
			options.headers = options.headers || {},
				options.method = 'POST',
				options.body = formData;
			// pend.value = true;
		},
		onResponse({ request, response, options }) {
			lab_nars_tech.value = response._data.doc_finish_2_6[2]
			stu_lab_tech.value = response._data.doc_finish_2_6[3]
			usl_nars_tech.value = response._data.doc_finish_2_6[4]
			stu_usl_tech.value = response._data.doc_finish_2_6[5]
			return response._data
		}
	})
	// pend.value = false;
	return;
}

await get_tech_report_6();

async function get_report_2() {
	let formData = new FormData();
	formData.append("mpp", props.mpp_doc)
	formData.append("dn", props.dn)
	formData.append("dk", props.dk)
	formData.append("opl", props.opl)
	formData.append("year", props.year)
	formData.append("month", props.month)
	formData.append("lgot", props.lgot)

	const { data: nar_tech } = await useFetch(urlHome.value + 'naryad/get_stat_for_zav2/' + props.mpp_doc + '/2/' + props.podrSelect + '/' + mpp.value + '/', {
		onRequest({ request, options }) {
			options.headers = options.headers || {},
				options.method = 'POST',
				options.body = formData;
			// pend.value = true;
		},
		onResponse({ request, response, options }) {
			lab_finish_all.value = response._data.doc_finish_2_6[2]
			addLabSum(lab_finish_all.value.reduce((x, y) => x + y.stu, 0))
			// stu_lab_finish.value = nars_list.value.reduce((accumulator, currentValue) => accumulator + currentValue.stu, 0);
			// stu_lab_finish.value = response._data.doc_finish_2_6[3]
			usl_finish_all.value = response._data.doc_finish_2_6[4]
			addUslSum(usl_finish_all.value.reduce((x, y) => x + y.stu, 0))
			// stu_usl_finish.value = response._data.doc_finish_2_6[5]
			return response._data
		}
	})
	// pend.value = false;
	return;
}

await get_report_2();

function contains(where, what) {
	for (var i = 0; i < what.length; i++) {
		if (where.indexOf(what[i]) == -1) return false;
	}
	return true;
}

async function finishNaryad(nkv) {
	const { data: finish_message } = await useFetch(urlHome.value + 'naryad/finish_nar_by_st/' + nkv + '/1/')
	if (finish_message.value.status === 'ok') {
		get_tech_report_6();
		get_report_2();
	}
	else {
		console.log(finish_message.value.message)
	}
}

function childFinish() {
	get_tech_report_6();
	get_report_2();
}

countLoadingDesc();

</script>

<template>
	<div v-show="(lab_nars_tech.length + usl_nars_tech.length + lab_finish_all.length + usl_finish_all.length) > 0">
		<details class="card">
			<summary class="card-header">
				<div v-show="(lab_nars_tech.length + usl_nars_tech.length + lab_finish_all.length + usl_finish_all.length) > 0"
					class="text-2xl font-medium border-double border-4 border-grey-400">
					<div class="grid grid-cols-2 gap-4 max-w-fit">
						<div
							v-if="lab_finish_all.length + usl_finish_all.length > 0 && + lab_nars_tech.length + usl_nars_tech.length === 0">
							✅
						</div>
						<div v-else>
							⚠️
						</div>

						<div><b>{{ doc }}</b></div>


					</div>
					<div class="grid grid-cols-3 gap-4 max-w-fit">
						<div>нарядов: <b>{{ lab_nars_tech.length + usl_nars_tech.length +
		lab_finish_all.length + usl_finish_all.length }}</b>
						</div>
						<div class="col-span-2">на сумму: <b>{{ Number((lab_nars_tech.reduce((s, x) => s + x.stu, 0) +
		usl_nars_tech.reduce((s, x) => s + x.stu, 0) + lab_finish_all.reduce((s, x) => s +
			x.stu, 0)
		+
		usl_finish_all.reduce((s, x) => s + x.stu, 0)).toFixed(2)).toLocaleString() }}
							</b>
							руб.
						</div>
					</div>
					<div class="text-xl">Закрыто нарядов с участием лаборатории: <b>{{ lab_nars_tech.length +
		lab_finish_all.length }}</b> на сумму <b>{{
		Number(((lab_nars_tech.reduce((s, x) => s + x.stu, 0)) +
			(lab_finish_all.reduce((s, x) => s + x.stu, 0))).toFixed(2)).toLocaleString() }}</b> руб.
					</div>

					<div v-if="lab_nars_tech.length + usl_nars_tech.length > 0">Наряды для проверки старшими техниками:
						{{
		lab_nars_tech.length + usl_nars_tech.length }} на
						сумму <b>{{ Number(((lab_nars_tech.reduce((s, x) => s + x.stu, 0)) + (usl_nars_tech.reduce((s,
		x) =>
		s +
		x.stu, 0))).toFixed(2)).toLocaleString() }}</b> руб.</div>
					<div v-if="lab_finish_all.length + usl_finish_all.length > 0">Закрытые наряды: <b>{{
		lab_finish_all.length +
		usl_finish_all.length }}</b> на <b>{{ Number((lab_finish_all.reduce((s, x) => s + x.stu, 0)
		+
		usl_finish_all.reduce((s, x) => s + x.stu, 0)).toFixed(2)).toLocaleString()
							}}</b>
						руб.
					</div>
				</div>
			</summary>
			<div v-if="(lab_nars_tech.length + usl_nars_tech.length + lab_finish_all.length + usl_finish_all.length) > 0"
				class=" border-double border-4">
				<!-- <div v-if="pend">
                <LoadFetchAnimation></LoadFetchAnimation>
            </div> -->

				<div v-if="lab_nars_tech.length + usl_nars_tech.length > 0"
					class="text-2xl font-semibold text-purple-800 drop-shadow-md shadow-black-600">Наряды
					для проверки старшими техниками: {{
		lab_nars_tech.length + usl_nars_tech.length }} на
					сумму <b>{{ Number(((lab_nars_tech.reduce((s, x) => s + x.stu, 0)) + (usl_nars_tech.reduce((s, x) =>
		s +
		x.stu, 0))).toFixed(2)).toLocaleString() }}</b> руб.</div>
				<div v-auto-animate>
					<div v-for="nar in lab_nars_tech" :key="nar.nkv"
						class="my-2 py-1 px-1 text-xs border-dotted border-2 border-indigo-600">
						<div>
							Наряд: <b>{{ nar.nkv }}</b>, Сумма: <b>{{ Number(nar.stu.toFixed(2)).toLocaleString() }}
								руб.</b>,
							Дата
							наряда: {{ dayjs(nar.dou).format('DD.MM.YYYY') }}, Дата закрытия: {{
		dayjs(nar.pdzr).format('DD.MM.YYYY')
	}},
							Пациент: <b>{{ nar.fam }} {{ nar.im }} {{ nar.ot }}</b>
						</div>
						<StNaryad @finish="childFinish" :nkv="nar.nkv" />
					</div>
				</div>
				<div v-if="usl_nars_tech.length > 0" class="relative flex py-5 items-center">
					<div class="flex-grow border-t border-gray-950"></div>
					<span class="flex-shrink mx-4 text-gray-950">Без лаборатории</span>
					<div class="flex-grow border-t border-gray-950"></div>
				</div>
				<div v-auto-animate>
					<div v-for="usl in usl_nars_tech" :key="usl.nkv">
						<UButton size="2xs" label="Верно" @click="finishNaryad(usl.nkv)" />
						Наряд: <b>{{ usl.nkv }}</b>, Сумма: <b>{{ Number(usl.stu.toFixed(2)).toLocaleString() }}
							руб.</b>,
						Дата
						наряда: {{ dayjs(usl.dou).format('DD.MM.YYYY') }}, Дата закрытия: {{
		dayjs(usl.dzr).format('DD.MM.YYYY')
	}},
						Пациент: <b>{{ usl.fam }} {{ usl.im }} {{ usl.ot }}</b>
					</div>
				</div>
				<div v-if="lab_finish_all.length + usl_finish_all.length > 0"
					class="text-2xl font-semibold text-emerald-600 drop-shadow-md shadow-black-600">Закрытые
					наряды: {{ lab_finish_all.length +
		usl_finish_all.length }} на <b>
						{{ Number(((lab_finish_all.reduce((s, x) => s + x.stu, 0)) +
		(usl_finish_all.reduce((s, x) => s + x.stu, 0))).toFixed(2)).toLocaleString() }}</b> руб.
				</div>
				<div v-auto-animate>
					<div v-for="nar in lab_finish_all" :key="nar.nkv">
						Наряд: <b>{{ nar.nkv }}</b>, Сумма: <b>{{ Number(nar.stu.toFixed(2)).toLocaleString() }}
							руб.</b>,
						Дата
						наряда: {{ dayjs(nar.dou).format('DD.MM.YYYY') }}, Дата закрытия: {{
		dayjs(nar.pdzr).format('DD.MM.YYYY')
	}},
						Пациент: <b>{{ nar.fam }} {{ nar.im }} {{ nar.ot }}</b>
					</div>
				</div>
				<div v-if="usl_finish_all.length > 0" class="relative flex py-5 items-center">
					<div class="flex-grow border-t border-gray-950"></div>
					<span class="flex-shrink mx-4 text-gray-950">Без лаборатории</span>
					<div class="flex-grow border-t border-gray-950"></div>
				</div>

				<div v-auto-animate>
					<div v-for="usl in usl_finish_all" :key="usl.nkv">
						Наряд: <b>{{ usl.nkv }}</b>, Сумма: <b>{{ Number(usl.stu.toFixed(2)).toLocaleString() }}
							руб.</b>,
						Дата
						наряда: {{ dayjs(usl.dou).format('DD.MM.YYYY') }}, Дата закрытия: {{
		dayjs(usl.dzr).format('DD.MM.YYYY')
						}},
						Пациент: <b>{{ usl.fam }} {{ usl.im }} {{ usl.ot }}</b>
					</div>
				</div>
			</div>
		</details>
	</div>
</template>